/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.core;

import commons.util.ArrayUtil;
import commons.util.MathUtil;

/**
 *
 *
 * @version 1.3.0
 * @author Susanta Tewari
 * @history Created on 2/15/13
 * @since 1.3.0
 */
public class Util {

    private Util() {}

    /**
     * summing across the (first) sampling dimension
     * @param data
     * @return
     */
    public static int[] getSampleTotal(final int[][] data) {

        final int[] result = new int[data[0].length];

        for (int i = 0; i < result.length; i++) {

            for (int[] aData : data) {
                result[i] += aData[i];
            }
        }

        return result;
    }

    /**
     * summing accross the first dimension (sample dimension(h)).
     * @param data
     * @return
     */
    public static int[][] getSampleTotal(final int[][][] data) {

        final int[][] result = new int[data[0].length][data[0][0].length];

        for (int i = 0; i < data[0].length; i++) {

            for (int j = 0; j < data[0][0].length; j++) {

                for (int[][] aData : data) {
                    result[i][j] += aData[i][j];
                }
            }
        }

        return result;
    }

    public static int[] getRowSums(final int[][] data) {

        final int[] result = new int[data.length];

        for (int i = 0; i < result.length; i++) {
            result[i] = (int) MathUtil.sum(data[i]);
        }

        return result;
    }

    public static int[] getColSums(final int[][] data) {

        final int[] result = new int[data[0].length];

        for (int i = 0; i < result.length; i++) {
            result[i] = (int) MathUtil.sum(ArrayUtil.getColumn(data, i));
        }

        return result;
    }
}
